package com.ktgj.customer.service.impl;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.alibaba.nacos.shaded.org.checkerframework.checker.units.qual.A;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.ktgj.common.core.annotation.Excel;
import com.ktgj.common.core.constant.BusinessTypeConstants;
import com.ktgj.common.core.constant.InsuranceTypeEnum;
import com.ktgj.common.core.enums.EmHelper;
import com.ktgj.customer.domain.*;
import com.ktgj.customer.domain.vo.*;
import com.ktgj.customer.mapper.*;
import com.ktgj.customer.service.ICustomerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ktgj.customer.service.IGuaranteeService;

/**
 * 保险
Service业务层处理
 * 
 * @author ruoyi
 * @date 2021-07-27
 */
@Service
public class GuaranteeServiceImpl implements IGuaranteeService 
{
    @Autowired
    private GuaranteeMapper guaranteeMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private InsuranceCompanyMapper insuranceCompanyMapper;
    @Autowired
    private ICustomerService customerService;
    @Autowired
    private InsuranceRecordMapper insuranceRecordMapper;
    @Autowired
    private InsuranceTypeMapper insuranceTypeMapper;

    /**
     * 查询保险

     * 
     * @param guaranteeId 保险
ID
     * @return 保险

     */
    @Override
    public Guarantee selectGuaranteeById(Long guaranteeId)
    {
        return guaranteeMapper.selectGuaranteeById(guaranteeId);
    }

    /**
     * 查询保险
列表
     * 
     * @param guarantee 保险

     * @return 保险

     */
    @Override
    public List<Guarantee> selectGuaranteeList(Guarantee guarantee)
    {
        return guaranteeMapper.selectGuaranteeList(guarantee);
    }

    /**
     * 新增保险

     * 
     * @param guarantee 保险

     * @return 结果
     */
    @Override
    public int insertGuarantee(Guarantee guarantee)
    {
        return guaranteeMapper.insertGuarantee(guarantee);
    }

    /**
     * 修改保险

     * 
     * @param guarantee 保险

     * @return 结果
     */
    @Override
    public int updateGuarantee(Guarantee guarantee)
    {
        return guaranteeMapper.updateGuarantee(guarantee);
    }

    /**
     * 批量删除保险

     * 
     * @param guaranteeIds 需要删除的保险
ID
     * @return 结果
     */
    @Override
    public int deleteGuaranteeByIds(Long[] guaranteeIds)
    {
        return guaranteeMapper.deleteGuaranteeByIds(guaranteeIds);
    }

    /**
     * 删除保险
信息
     * 
     * @param guaranteeId 保险
ID
     * @return 结果
     */
    @Override
    public int deleteGuaranteeById(Long guaranteeId)
    {
        return guaranteeMapper.deleteGuaranteeById(guaranteeId);
    }

    /**
     * 我的-订单列表（保险）
     * */
    @Override
    public List<GuaranteeVo> selectGuarnteeByCustomerId(Long customerId) {
        List<Guarantee> guaranteeList = guaranteeMapper.selectGuarnteeByCustomerId(customerId);
        Customer customer=customerMapper.selectCustomerById(customerId);

        List<GuaranteeVo> guaranteeVos = new ArrayList<>();
        for (Guarantee guarantee : guaranteeList) {
            InsuranceCompany insuranceCompany=insuranceCompanyMapper.selectInsuranceCompanyById(guarantee.getInsCoId());
            GuaranteeVo guaranteeVo=new GuaranteeVo();
            guaranteeVo.setAmount(guarantee.getAmount());
            guaranteeVo.setCustomerName(customer.getCustomerName());
            guaranteeVo.setGuaranteedate(guarantee.getGuaranteedate());
            guaranteeVo.setGuaranteeStatus(StateType(guarantee.getGuaranteeStatus()));
            guaranteeVo.setInsCoName(insuranceCompany.getInsCoName());
            guaranteeVos.add(guaranteeVo);
        }
        return guaranteeVos;
    }

    /**
     * 保险信息列表
     * */
    @Override
    public List<InsuranceRecord> guaranteeList(TmpGuaranteeVo tmpGuaranteeVo) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        CustomerIdVo customerIdVo = new CustomerIdVo();
        if (tmpGuaranteeVo.getName() != null){
            customerIdVo.setCustomerName(tmpGuaranteeVo.getName());
        }
        if (tmpGuaranteeVo.getPhone() != null){
            customerIdVo.setCustomerPhone(tmpGuaranteeVo.getPhone());
        }
        if ((customerIdVo.getCustomerName() != null && !customerIdVo.getCustomerName().equals("")) ||
                (customerIdVo.getCustomerPhone() != null && !customerIdVo.getCustomerPhone().equals(""))){
            try {
            customerIdVo = customerService.customerId(customerIdVo);
            } catch (Exception e){
                throw new Exception(e.getMessage());
            }
            if (customerIdVo != null){
                tmpGuaranteeVo.setCustomerId(customerIdVo.getCustomerId());
            }
        }

        List<InsuranceRecord> insuranceRecordList = insuranceRecordMapper.openInsuranceList(tmpGuaranteeVo);
        if (insuranceRecordList.size() > 0){
            for (InsuranceRecord insuranceRecord : insuranceRecordList) {

                if (insuranceRecord.getOpenState().equals(InsuranceTypeEnum.Open.getValue())){
                    insuranceRecord.setOpenStateName("已开通");
                }
                else if (insuranceRecord.getOpenState().equals(InsuranceTypeEnum.Maturity.getValue())){
                    insuranceRecord.setOpenStateName("已到期");
                }
                else if (insuranceRecord.getOpenState().equals(InsuranceTypeEnum.CheckOut.getValue())){
                    insuranceRecord.setOpenStateName("已退卡");
                }
                else if (insuranceRecord.getOpenState().equals(InsuranceTypeEnum.FailedToActivate.getValue())){
                    insuranceRecord.setOpenStateName("开通失败");
                }
                else if (insuranceRecord.getOpenState().equals(InsuranceTypeEnum.Suspend.getValue())){
                    insuranceRecord.setOpenStateName("暂缓");
                }

                InsuranceType insuranceType = insuranceTypeMapper.selectInsuranceTypeById(insuranceRecord.getInsTypeId());
                insuranceRecord.setInsTypeName(insuranceType.getInsTypeName());

                InsuranceCompany insuranceCompany = insuranceCompanyMapper.selectInsuranceCompanyById(insuranceRecord.getInsCoId());
                insuranceRecord.setInsCoName(insuranceCompany.getInsCoName());

                if (insuranceRecord.getGuaranteeId() != null && insuranceRecord.getGuaranteeId() != 0){
                    Guarantee guarantee = guaranteeMapper.selectGuaranteeById(insuranceRecord.getGuaranteeId());
                    insuranceRecord.setCardNum(guarantee.getCardId());
                }

                insuranceRecord.setInsValidTime(format.format(insuranceRecord.getInsValidDate()));
                insuranceRecord.setInsInvalidTime(format.format(insuranceRecord.getInsInvalidDate()));

                CustomerIdVo customerId = new CustomerIdVo();
                customerId.setCustomerId(insuranceRecord.getCustomerId());
                customerId = customerService.customerId(customerId);
                if (customerId != null){
                    insuranceRecord.setCustomerName(customerId.getCustomerName());
                    insuranceRecord.setIdNum(customerId.getLicenseNum());
                }

                if (insuranceRecord.getOrderSource().equals(BusinessTypeConstants.customer.getValue())){
                    insuranceRecord.setOrderSourceName("会员");
                }
                else if (insuranceRecord.getOrderSource().equals(BusinessTypeConstants.flight.getValue())){
                    insuranceRecord.setOrderSourceName("机票");
                }
                else if (insuranceRecord.getOrderSource().equals(BusinessTypeConstants.hotel.getValue())){
                    insuranceRecord.setOrderSourceName("酒店");
                }
                else if (insuranceRecord.getOrderSource().equals(BusinessTypeConstants.viphall.getValue())){
                    insuranceRecord.setOrderSourceName("贵宾厅");
                }
                else if (insuranceRecord.getOrderSource().equals(BusinessTypeConstants.train.getValue())){
                    insuranceRecord.setOrderSourceName("火车票");
                }
                else if (insuranceRecord.getOrderSource().equals(BusinessTypeConstants.travel.getValue())){
                    insuranceRecord.setOrderSourceName("旅游");
                }
                else if (insuranceRecord.getOrderSource().equals(BusinessTypeConstants.health.getValue())){
                    insuranceRecord.setOrderSourceName("健康");
                }
                else if (insuranceRecord.getOrderSource().equals(BusinessTypeConstants.shopping.getValue())){
                    insuranceRecord.setOrderSourceName("商城");
                }
                else if (insuranceRecord.getOrderSource().equals(BusinessTypeConstants.specialCar.getValue())){
                    insuranceRecord.setOrderSourceName("专车");
                }
            }
        }
        return insuranceRecordList;
    }

    /**
     * 查询待修改保险信息
     * */
    @Override
    public SelGuaranteeVo selGuarantee(Long id) throws Exception {
        SelGuaranteeVo selGuaranteeVo = new SelGuaranteeVo();
        InsuranceRecord insuranceRecord = insuranceRecordMapper.selectInsuranceRecord(id);
        Guarantee guarantee = guaranteeMapper.selGuarantee(insuranceRecord.getGuaranteeId());
        if (guarantee != null){
            selGuaranteeVo.setId(guarantee.getGuaranteeId());
            selGuaranteeVo.setCardNum(guarantee.getCardId());

            CustomerIdVo customerIdVo = new CustomerIdVo();
            customerIdVo.setCustomerId(guarantee.getCustomerId());
            try {
                customerIdVo = customerService.customerId(customerIdVo);
            } catch (Exception e){
                throw new Exception(e.getMessage());
            }

            if (customerIdVo != null){
                selGuaranteeVo.setCustomerName(customerIdVo.getCustomerName());
            }
            selGuaranteeVo.setBirthday(guarantee.getBirthday());
            selGuaranteeVo.setSex(guarantee.getSex());
            selGuaranteeVo.setCertificateType(guarantee.getCertificateType());
            selGuaranteeVo.setLicenseNumber(guarantee.getLicenseNumber());
            selGuaranteeVo.setPhone(guarantee.getPhone());
            selGuaranteeVo.setGuaranteeNo(insuranceRecord.getInsSlipNo());
            selGuaranteeVo.setMembership(guarantee.getMembership());
        }
        return selGuaranteeVo;
    }

    /**
     * 修改保险信息
     * */
    @Override
    public int upGuarantee(SelGuaranteeVo selGuaranteeVo) throws Exception {
        CustomerIdVo customerIdVo = new CustomerIdVo();
        if (selGuaranteeVo.getCustomerName() != null){
            customerIdVo.setCustomerName(selGuaranteeVo.getCustomerName());
            try {
                customerIdVo = customerService.customerId(customerIdVo);
            } catch (Exception e){
                throw new Exception(e.getMessage());
            }
        }
        Guarantee guarantee = new Guarantee();
        guarantee.setGuaranteeId(selGuaranteeVo.getId());
        guarantee.setCardId(selGuaranteeVo.getCardNum());
        if (customerIdVo != null){
            guarantee.setCustomerId(customerIdVo.getCustomerId());
        }
        guarantee.setGuaranteeNo(selGuaranteeVo.getGuaranteeNo());
        guarantee.setBirthday(selGuaranteeVo.getBirthday());
        guarantee.setSex(selGuaranteeVo.getSex());
        guarantee.setCertificateType(selGuaranteeVo.getCertificateType());
        guarantee.setLicenseNumber(selGuaranteeVo.getLicenseNumber());
        guarantee.setPhone(selGuaranteeVo.getPhone());
        guarantee.setMembership(selGuaranteeVo.getMembership());
        int i = guaranteeMapper.updateGuarantee(guarantee);
        return i;
    }

    /**
     * 保险详情
     * */
    @Override
    public InsuranceDetailsVo insuranceDetails(Long id) throws Exception {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat time = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        InsuranceDetailsVo insuranceDetailsVo = new InsuranceDetailsVo();
        Guarantee guarantee = guaranteeMapper.selGuarantee(id);
        if (guarantee != null){
            CustomerIdVo customerIdVo = new CustomerIdVo();
            customerIdVo.setCustomerId(guarantee.getCustomerId());
            customerIdVo = customerService.customerId(customerIdVo);
            Customer customer = customerMapper.InquireCustomerById(guarantee.getCustomerId());
            if (customerIdVo != null){
                insuranceDetailsVo.setName(customerIdVo.getCustomerName());
                insuranceDetailsVo.setCardNum(customerIdVo.getCustomerCard());
                insuranceDetailsVo.setPhone(customerIdVo.getCustomerPhone());
            }
            if (customer != null){
                if (customer.getCustomerSex() != null){
                    if (customer.getCustomerSex().equals("male")){
                        insuranceDetailsVo.setSex("男");
                    }
                    else if (customer.getCustomerSex().equals("female")){
                        insuranceDetailsVo.setSex("女");
                    }
                }
                if (customer.getBirthday() != null){
                    insuranceDetailsVo.setBirthday(format.format(customer.getBirthday()));
                }

                if (customer.getIdType() != null){
                    if (customer.getIdType().equals(EmHelper.CertificateType.ID_CARD.getValue())){
                        insuranceDetailsVo.setCertificateType("身份证");
                    }
                    else if (customer.getIdType().equals(EmHelper.CertificateType.MILITARY_ID.getValue())){
                        insuranceDetailsVo.setCertificateType("军人证");
                    }
                    else if (customer.getIdType().equals(EmHelper.CertificateType.PASSPORT.getValue())){
                        insuranceDetailsVo.setCertificateType("护照");
                    }
                    else if (customer.getIdType().equals(EmHelper.CertificateType.MTPFHKAMR.getValue())){
                        insuranceDetailsVo.setCertificateType("港澳居民来往内地通行证");
                    }
                    else if (customer.getIdType().equals(EmHelper.CertificateType.MTPFTR.getValue())){
                        insuranceDetailsVo.setCertificateType("台湾居民来往大陆通行证");
                    }
                    else if (customer.getIdType().equals(EmHelper.CertificateType.RPFHKMATR.getValue())){
                        insuranceDetailsVo.setCertificateType("港澳台居民居住证");
                    }
                    else if (customer.getIdType().equals(EmHelper.CertificateType.PRICFF.getValue())){
                        insuranceDetailsVo.setCertificateType("外国人永久居留身份证");
                    }
                    else if (customer.getIdType().equals(EmHelper.CertificateType.HKAMP.getValue())){
                        insuranceDetailsVo.setCertificateType("港澳通行证");
                    }
                    else if (customer.getIdType().equals(EmHelper.CertificateType.ACCOUNT_BOOK.getValue())){
                        insuranceDetailsVo.setCertificateType("户口本");
                    }
                    else if (customer.getIdType().equals(EmHelper.CertificateType.BIRTH_CERTIFICATE.getValue())){
                        insuranceDetailsVo.setCertificateType("出生证明");
                    }
                    else if (customer.getIdType().equals(EmHelper.CertificateType.TAIWAN_PASS.getValue())){
                        insuranceDetailsVo.setCertificateType("台湾通行证");
                    }
                }
                insuranceDetailsVo.setCertificateNum(customer.getCustCertificateNo());
            }
            insuranceDetailsVo.setRelation(guarantee.getMembership());

            InsuranceType insuranceType = insuranceTypeMapper.selectInsuranceType(guarantee.getInsTypeId());
            if (insuranceType != null){
                insuranceDetailsVo.setInsuranceType(insuranceType.getInsTypeName());
            }

            InsuranceCompany tmpInsuranceCompany = insuranceCompanyMapper.insuranceCompany(guarantee.getInsCoId());
            if (tmpInsuranceCompany != null){
                insuranceDetailsVo.setInsuranceCompany(tmpInsuranceCompany.getInsCoName());
            }

            if (guarantee.getCreateTime() != null){
                insuranceDetailsVo.setInsuredDate(time.format(guarantee.getCreateTime()));
            }
            insuranceDetailsVo.setPolicyNum(guarantee.getGuaranteeNo());

            InsuranceRecord insuranceRecord = insuranceRecordMapper.selectInsuranceRecord(guarantee.getGuaranteeId());
            if (insuranceRecord != null){
                if (insuranceRecord.getInsValidDate() != null){
                    insuranceDetailsVo.setTakeEffectDate(time.format(insuranceRecord.getInsValidDate()));
                }
                if (insuranceRecord.getInsInvalidDate() != null){
                    insuranceDetailsVo.setInvalidDate(time.format(insuranceRecord.getInsInvalidDate()));
                }
                insuranceDetailsVo.setePolicyNum(insuranceRecord.getElectronInsNo());
                insuranceDetailsVo.setErrorMessage(insuranceRecord.getInsMemo());
            }
            if (guarantee.getCreateTime() != null){
                insuranceDetailsVo.setCreationTime(time.format(guarantee.getCreateTime()));
            }
            return insuranceDetailsVo;
        }
        else {
            throw new Exception("无效的id");
        }
    }

    /**
     * 保险状态类型
     */
    public String StateType(String guaranteeStatus){
        String str=null;
        switch (guaranteeStatus){
            case "Initial" :
                str="初始";
                break;
            case "Sold" :
                str="已售出";
                break;
            case "Open" :
                str="已开通";
                break;
            case "CheckOut" :
                str="已退卡";
                break;
            case "Maturity" :
                str="已到期";
                break;
            case "Locking" :
                str="已锁定";
                break;
        }
        return str;
    }
}
